package com.justech.eat.global;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @program: mes
 * @description: 关于跨域访问的设置
 * @author: feifei.li
 * @create: 2019-09-09 11:12
 **/
@Configuration
public class CorsConfig {

    @Bean
    public CorsConfiguration corsConfiguration() {
        CorsConfiguration configuration = new CorsConfiguration();
        /*实际请求中允许携带的首部字段*/
        configuration.addAllowedHeader("*");
        /*允许那些域跨域访问*/
        configuration.addAllowedOrigin("*");
        /*允许那些请求方法*/
        configuration.addAllowedMethod("*");
        return configuration;
    }

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", corsConfiguration());
        return new CorsFilter(source);
    }
}
